package com.fr.base.core.util;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Ascii85OutputStream extends FilterOutputStream
{
  private int line_break = this.maxline = 72;
  private int count = 0;
  private byte[] indata = new byte[4];
  private byte[] outdata = new byte[5];
  private int maxline;
  private boolean flushed = false;
  private char terminator = '~';

  public Ascii85OutputStream(OutputStream paramOutputStream)
  {
    super(paramOutputStream);
  }

  public void setTerminator(char paramChar)
  {
    if ((paramChar < 'v') || (paramChar > '~') || (paramChar == 'z'))
      throw new IllegalArgumentException("Terminator must be 118-126 excluding z");
    this.terminator = paramChar;
  }

  public char getTerminator()
  {
    return this.terminator;
  }

  public void setLineLength(int paramInt)
  {
    if (this.line_break > paramInt)
      this.line_break = paramInt;
    this.maxline = paramInt;
  }

  public int getLineLength()
  {
    return this.maxline;
  }

  private final void Ascii85Tuple()
  {
    long l1 = ((this.indata[0] << 8 | this.indata[1] & 0xFF) << 16 | (this.indata[2] & 0xFF) << 8 | this.indata[3] & 0xFF) & 0xFFFFFFFF;
    if (l1 == 0L)
    {
      this.outdata[0] = 122;
      this.outdata[1] = 0;
      return;
    }
    long l2 = l1 / 52200625L;
    this.outdata[0] = (byte)(int)(l2 + 33L);
    l1 -= l2 * 52200625L;
    l2 = l1 / 614125L;
    this.outdata[1] = (byte)(int)(l2 + 33L);
    l1 -= l2 * 614125L;
    l2 = l1 / 7225L;
    this.outdata[2] = (byte)(int)(l2 + 33L);
    l1 -= l2 * 7225L;
    l2 = l1 / 85L;
    this.outdata[3] = (byte)(int)(l2 + 33L);
    this.outdata[4] = (byte)(int)(l1 % 85L + 33L);
  }

  public final void write(int paramInt)
    throws IOException
  {
    this.flushed = false;
    this.indata[(this.count++)] = (byte)paramInt;
    if (this.count < 4)
      return;
    Ascii85Tuple();
    for (int i = 0; i < 5; ++i)
    {
      if (this.outdata[i] == 0)
        break;
      this.out.write(this.outdata[i]);
      if (--this.line_break == 0)
      {
        this.out.write(10);
        this.line_break = this.maxline;
      }
    }
    this.count = 0;
  }

  public final void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    for (int i = 0; i < paramInt2; ++i)
      if (this.count < 3)
        this.indata[(this.count++)] = paramArrayOfByte[(paramInt1 + i)];
      else
        write(paramArrayOfByte[(paramInt1 + i)]);
  }

  public final void flush()
    throws IOException
  {
    int i;
    if (this.flushed)
      return;
    if (this.count > 0)
    {
      for (i = this.count; i < 4; ++i)
        this.indata[i] = 0;
      Ascii85Tuple();
      if (this.outdata[0] == 122)
        for (i = 0; i < 5; ++i)
          this.outdata[i] = 33;
      for (i = 0; i < this.count + 1; ++i)
      {
        this.out.write(this.outdata[i]);
        if (--this.line_break == 0)
        {
          this.out.write(10);
          this.line_break = this.maxline;
        }
      }
    }
    if (--this.line_break == 0)
      this.out.write(10);
    this.out.write(this.terminator);
    this.out.write(10);
    this.count = 0;
    this.line_break = this.maxline;
    this.flushed = true;
    super.flush();
  }

  public void close()
    throws IOException
  {
    try
    {
      super.close();
    }
    finally
    {
      this.indata = (this.outdata = null);
    }
  }

  protected void finalize()
    throws Throwable
  {
    try
    {
      flush();
    }
    finally
    {
      finalize();
    }
  }
}